package com.sskj.jingbi.presenter;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.DisposUtil;

import com.sskj.jingbi.http.HttpConfig;
import com.sskj.jingbi.presenter.BasePresenter;
import com.sskj.jingbi.ui.activity.MainActivity;
import com.sskj.lib.bean.AppVersionBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.GonggaoBean;
import com.sskj.lib.http.JsonCallBack;

import io.reactivex.disposables.Disposable;


public class MainActivityPresenter extends BasePresenter<MainActivity> {
    public void getGonggao() {
        httpService.getGonggao()
                .execute(new JsonCallBack<HttpData<GonggaoBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<GonggaoBean>> response) {
                        HttpData<GonggaoBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.showGonggao(httpData.getData());

                        }
                    }
                });
    }
    private WebSocketObserver stockSocket;

    private Disposable stockDisposable;

    public void initWebSocket() {
        if (stockSocket == null) {
            stockSocket = httpService.pushCoinType();
            stockSocket
                    .map(s -> new Gson().fromJson(s, CoinBean.class))
                    .subscribe(coinBean -> RxBus.getDefault().post(coinBean), e -> {
                    });

        } else {
//            stockSocket.send(new Gson().toJson(new SocketSendBean(Constans.NO_FIVE_PUSH)));
//            stockSocketFive.send(new Gson().toJson(new SocketSendBean(Constans.FIVE_PUSH)));
        }

    }


    public void closeWebSocket() {
        stockSocket = null;

        DisposUtil.close(stockDisposable);
    }

    public void getNewVersion() {
        httpService.getVersion()
                .execute(new JsonCallBack<HttpData<AppVersionBean>>(false) {
                    @Override
                    public void onSuccess(Response<HttpData<AppVersionBean>> response) {
                        HttpData<AppVersionBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.onVersionDataSuccess(httpData.getData());
                        }else {
//                            getGonggao();
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<AppVersionBean>> response) {
//                        getGonggao();
                        super.onError(response);
                    }
                });
    }

}
